package com.lottery.game.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import lombok.extern.slf4j.Slf4j;

import static org.apache.commons.lang3.time.DateUtils.addDays;

/**
 * @author : yinlin
 * @version : 1.0
 * @date : 2019-02-20 20:24
 * @Description :
 * @Copyright : Copyright (c) 2018
 * @Company : KeRuYun Technology(Beijing) Chengdu Co. Ltd.
 * @link : com.runshang.apps.cloudgame.commons.base.utils.DateUtils
 */
@Slf4j
public class DateUtils {

    public static boolean isCurrentTimeIn(String begin, String end) {
        if (begin.equals(end)) {
            return true;
        }
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        Date now = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            now = df.parse(df.format(new Date()));
            beginTime = df.parse(begin);
            endTime = df.parse(end);
            if (endTime.before(beginTime)) {
                endTime = addDays(endTime, 1);
            }
        } catch (Exception e) {
           log.error(e.getMessage(), e);
        }

        return belongCalendar(now, beginTime, endTime);

    }

    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static String parseDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return sdf.format(date);
    }

    public static String getNowDateString() {
        Date date = new Date();
        return parseDate(date);
    }

    public static String timeStamp2Date(long seconds) {
        if (seconds == 0 || seconds < 0) {
            return "";
        }
        return DateUtils.parseDate(new Date(seconds));
    }

    public static String getDateString(Date date) {
        if(date==null)
            return null;
        return parseDate(date);
    }

}
